package org.springblade.modules.customer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.PageVO;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.customer.entity.Customer;
import org.springblade.modules.customer.form.CustomerForm;
import org.springblade.modules.customer.service.ICustomerService;
import org.springblade.modules.customer.vo.CustomerVO;
import org.springblade.modules.customer.wrapper.CustomerWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 客户表 控制器
 *
 * @author armyant
 * @since 2024-04-21
 */
@RestController
@AllArgsConstructor
@Validated
@RequestMapping(CommonConstant.APPLICATION_CUSTOMER_NAME + "/customer")
@Api(tags = "客户表", value = "前端控制")
public class CustomerController extends BladeController {

	private final ICustomerService customerService;

	@GetMapping("/detail")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "详情", notes = "根据主键获取数据")
	@ApiImplicitParams({@ApiImplicitParam(name = "id", required = true, dataType = "Long", value = "主键,不能为空，不能小于1")})
	public R<CustomerVO> detail(@NotNull @Min(value = 1) @RequestParam Long id) {
		return R.data(CustomerWrapper.build().entityVO(customerService.getById(id)));
	}

	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "客户编码", paramType = "query", dataType = "String")
	})
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "分页查询", notes = "可以自定义查询条件")
	public R<PageVO<CustomerVO>> page(@ApiIgnore Customer condition, Query query) {
		// 查询条件
		LambdaQueryWrapper<Customer> queryWrapper = Wrappers.<Customer>query().lambda();
		if (StringUtil.isNotBlank(condition.getCode())) {
			queryWrapper.like(Customer::getCode, condition.getCode());
		}
		IPage<Customer> pages = customerService.page(Condition.getPage(query), queryWrapper);
		return R.data(CustomerWrapper.build().pageVO(pages));
	}

	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "主键", paramType = "query", dataType = "long")
	})
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "查询所有", notes = "可以自定义查询条件")
	public R<List<CustomerVO>> list(@ApiIgnore Customer condition) {
		// 查询条件
		LambdaQueryWrapper<Customer> queryWrapper = Wrappers.<Customer>query().lambda();
		List<Customer> records = customerService.list(queryWrapper);
		return R.data(CustomerWrapper.build().listVO(records));
	}

	@PostMapping("/submit")
	@ApiOperationSupport(order = 21)
	@ApiOperation(value = "新增", notes = "保存数据")
	public R<String> save(@Valid @RequestBody CustomerForm form) {
		customerService.save(form);
		return R.success();
	}

	@PostMapping("/update")
	@ApiOperationSupport(order = 31)
	@ApiOperation(value = "修改", notes = "修改数据")
	@ApiImplicitParams({@ApiImplicitParam(name = "id", required = true, dataType = "Long", value = "主键,不能为空，不能小于1")})
	public R<String> update(@Valid @RequestBody CustomerForm form, @NotNull @Min(value = 1) @RequestParam Long id) {
		customerService.editByPrimaryKey(form, id);
		return R.success();
	}

	@PostMapping("/remove")
	@ApiOperationSupport(order = 41)
	@ApiOperation(value = "删除", notes = "逻辑删除")
	@ApiImplicitParams({@ApiImplicitParam(name = "id", required = true, dataType = "Long", value = "主键,不能为空，不能小于1")})
	public R<String> remove(@NotNull @Min(value = 1) @RequestParam Long id) {
		customerService.deleteByPrimaryKey(id);
		return R.success();
	}

}
